﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class news_news : System.Web.UI.Page
{
    private string IDEN, NEWSPARAM;
    private int FLAGSEARCH = 1;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.IDEN = Request.Params.Get("id");
        this.NEWSPARAM = Request.Params.Get("news");
        if (!Page.IsPostBack)
        {
            if (this.NEWSPARAM != null)
            {
                MNews.ActiveViewIndex = 1;
            }
            else
            {
                MNews.ActiveViewIndex = 0;
            }
            intPageSize.Text = 8 + ""; //Here indicates the size of the page
            intCurrIndex.Text = 0 + "";
            Bind(this.txtSearch.Text);
        }        
    }

    #region Pager

    //Method to query data and create the pager
    private void Bind(String txtSearch)
    {
        DataSet objDS = new DataSet();
        clsNews objNews = new clsNews();

        if (!Page.IsPostBack)
        {
            objDS = objNews.SeeAllTheRecords(0, 0, txtSearch);
            intRecordCount.Text = objDS.Tables[0].Rows.Count + "";
            objDS = null;
            objDS = new DataSet();
        }

        if (intCurrIndex.Text.Equals("0"))
        {
            hrefPrevious.Visible = false;
        }
        else
        {
            hrefPrevious.Visible = true;
        }

        objDS = objNews.SeeAllTheRecords(Int32.Parse(intCurrIndex.Text), Int32.Parse(intPageSize.Text), txtSearch);
        if (FLAGSEARCH != 1)
        {
            intRecordCount.Text = objDS.Tables[0].Rows.Count + "";
            FLAGSEARCH = 1;
        }

        dataListNews.DataSource = objDS.Tables[0].DefaultView;
        dataListNews.DataBind();
        PrintStatus();
    }

    //Method to navigate to the first page of pager
    public void ShowFirst(object sender, EventArgs e)
    {
        intCurrIndex.Text = "0";
        Bind(this.txtSearch.Text);
    }

    //Method to navigate to the previous page in the pager
    public void ShowPrevious(object sender, EventArgs e)
    {
        intCurrIndex.Text = (Int32.Parse(intCurrIndex.Text) - Int32.Parse(intPageSize.Text)) + "";
        if (Int32.Parse(intCurrIndex.Text) < 0)
        {
            intCurrIndex.Text = "0";
        }
        Bind(this.txtSearch.Text);
    }

    //Method to navigate to the next page in the pager
    public void ShowNext(object sender, EventArgs e)
    {
        if ((Int32.Parse(intCurrIndex.Text) + 1) < Int32.Parse(intRecordCount.Text))
        {
            intCurrIndex.Text = (Int32.Parse(intCurrIndex.Text) + Int32.Parse(intPageSize.Text)) + "";
        }
        Bind(this.txtSearch.Text);
    }

    //Method to navigate to the last page of the pager
    public void ShowLast(object sender, EventArgs e)
    {
        int tmpInt = 0;
        tmpInt = Int32.Parse(intRecordCount.Text) % Int32.Parse(intPageSize.Text);
        if (tmpInt > 0)
        {
            intCurrIndex.Text = (Int32.Parse(intRecordCount.Text) - tmpInt) + "";
        }
        else
        {
            intCurrIndex.Text = (Int32.Parse(intRecordCount.Text) - Int32.Parse(intPageSize.Text)) + "";
        }
        Bind(this.txtSearch.Text);
    }

    //Method to generate the data pager navigation
    private void PrintStatus()
    {
        lblShowing.Text = "Pagina actual:<b> ";
        lblShowing.Text += ((Int32.Parse(intCurrIndex.Text) / Int32.Parse(intPageSize.Text)) + 1) + "";
        lblShowing.Text += "</b> of <b>";

        int pageActuality = ((Int32.Parse(intCurrIndex.Text) / Int32.Parse(intPageSize.Text)) + 1);
        int pageMaximun = 0;

        if ((Int32.Parse(intRecordCount.Text) % Int32.Parse(intPageSize.Text)) > 0)
        {
            lblShowing.Text += ((Int32.Parse(intRecordCount.Text) / Int32.Parse(intPageSize.Text)) + 1) + "";
            pageMaximun = ((Int32.Parse(intRecordCount.Text) / Int32.Parse(intPageSize.Text)) + 1);
        }
        else
        {
            lblShowing.Text += (Int32.Parse(intRecordCount.Text) / Int32.Parse(intPageSize.Text)) + "";
            pageMaximun = (Int32.Parse(intRecordCount.Text) / Int32.Parse(intPageSize.Text));
        }
        lblShowing.Text += "</b>";

        if (pageMaximun == pageActuality)
        {
            hrefNext.Visible = false;
        }
        else
        {
            hrefNext.Visible = true;
        }
    }
    #endregion

    //To search method
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        FLAGSEARCH = 2;
        Bind(this.txtSearch.Text);
    }
    protected void lnkPrevious_Click(object sender, EventArgs e)
    {
        MNews.ActiveViewIndex = 0;
    }
}
